from flask import Blueprint, render_template, request
from .order_manager import OrderManager

order_bp = Blueprint('order', __name__)

# 实例化 OrderManager
order_manager = OrderManager()

@order_bp.route('/myOrder', methods=['GET', 'POST'])
def my_order():
    username = request.args.get("username", "")  # 获取用户名
    if request.method == 'GET':
        cart_items = order_manager.get_shopping_cart(username)
        if cart_items:
            return render_template('myOrder.html', username=username, result=cart_items, messages="done")
        else:
            return render_template('myOrder.html', username=username, messages="none")

    action = request.form.get("action")
    if action == "加入购物车":
        restaurant = request.form['restaurant']
        dishname = request.form['dishname']
        price = float(request.form['price'])
        img_res = request.form['img_res']
        success = order_manager.add_to_cart(username, restaurant, dishname, price, img_res)
        cart_items = order_manager.get_shopping_cart(username)
        return render_template('myOrder.html', username=username, result=cart_items,
                               messages="done" if success else "fail")

    elif action == "结算":
        restaurant = request.form['restaurant']
        dishname = request.form['dishname']
        price = request.form['price']
        img_res = request.form['img_res']
        mode = request.form['mode']
        success = order_manager.checkout_order(username, restaurant, dishname, price, img_res, mode)
        return render_template('index.html')

    return render_template('index.html')


@order_bp.route('/OrderPage', methods=['GET', 'POST'])
def order_page():
    username = request.args.get("username", "")  # 获取用户名
    not_finished_num = len(order_manager.get_unfinished_orders(username) or [])
    if request.method == 'GET':
        orders = order_manager.get_order_comments(username)
        if orders:
            return render_template('OrderPage.html', username=username, result=orders, messages="done",
                                   notFinishedNum=not_finished_num)
        else:
            return render_template('OrderPage.html', username=username, messages="none")

    action = request.form.get("action")
    if action == "按时间排序":
        orders = order_manager.sort_orders_by_time(username)
        return render_template('OrderPage.html', username=username, result=orders, messages="done",
                               notFinishedNum=not_finished_num)

    elif action == "按价格排序":
        orders = order_manager.sort_orders_by_price(username)
        return render_template('OrderPage.html', username=username, result=orders, messages="done",
                               notFinishedNum=not_finished_num)

    elif action == "未完成订单":
        orders = order_manager.get_unfinished_orders(username)
        return render_template('OrderPage.html', username=username, result=orders, messages="done",
                               notFinishedNum=len(orders or []))

    elif action == "确认收货":
        orderID = request.form['orderID']
        dishname = request.form['dishname']
        restaurant = request.form['restaurant']
        success = order_manager.confirm_order(orderID, dishname, restaurant)
        return render_template('OrderPage.html', username=username, messages="UpdateSucceed" if success else "UpdateFailed")

    return render_template('OrderPage.html', username=username, messages="none")


@order_bp.route('/MyComments', methods=['GET', 'POST'])
def my_comments():
    username = request.args.get("username", "")  # 获取用户名
    not_finished_num = len(order_manager.get_unfinished_orders(username) or [])
    if request.method == 'GET':
        comments = order_manager.get_my_comments(username)
        if comments:
            return render_template('MyComments.html', username=username, result=comments, messages="done",
                                   notFinishedNum=not_finished_num)
        else:
            return render_template('MyComments.html', username=username, messages="none")

    return render_template('MyComments.html', username=username, messages="none")